<!DOCTYPE html>
<html>
    <head>
        <!-- 页面meta -->
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <title>传智健康</title>
        <meta name="description" content="传智健康">
        <meta name="keywords" content="传智健康">
        <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
        <!-- 引入样式 -->
        <link rel="stylesheet" href="../plugins/elementui/index.css">
        <link rel="stylesheet" href="../plugins/font-awesome/css/font-awesome.min.css">
        <link rel="stylesheet" href="../css/style.css">
        <style>
            .datatable {
                position: relative;
                box-sizing: border-box;
                -webkit-box-flex: 1;
                width: 100%;
                max-width: 100%;
                font-size: 14px;
                color: rgb(96, 98, 102);
                overflow: hidden;
                flex: 1 1 0%;
            }
            .datatable td, .datatable th {
                padding: 12px 0;
                min-width: 0;
                -webkit-box-sizing: border-box;
                box-sizing: border-box;
                text-overflow: ellipsis;
                vertical-align: middle;
                position: relative;
                text-align: left;
            }
        </style>
    </head>
    <body class="hold-transition">
        <div id="app">
            <div class="content-header">
                <h1>系统管理<small>角色管理</small></h1>
                <el-breadcrumb separator-class="el-icon-arrow-right" class="breadcrumb">
                    <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
                    <el-breadcrumb-item>系统管理</el-breadcrumb-item>
                    <el-breadcrumb-item>角色管理</el-breadcrumb-item>
                </el-breadcrumb>
            </div>
            <div class="app-container">
                <div class="box">
                    <div class="filter-container">
                        <el-input placeholder="角色名/关键字/描述" v-model="pagination.queryString" style="width: 200px;" class="filter-item"></el-input>
                        <el-button @click="findPage()" class="dalfBut">查询</el-button>
                        <el-button type="primary" class="butT" :disabled="permissionShow.ADD" @click="handleCreate()">新建</el-button>
                    </div>
                    <el-table size="small" current-row-key="id" :data="dataList" stripe highlight-current-row>
                        <el-table-column type="index" align="center" label="序号"></el-table-column>
                        <el-table-column prop="name" label="角色名" align="center"></el-table-column>
                        <el-table-column prop="keyword" label="关键字" align="center"></el-table-column>
                        <el-table-column prop="description" label="描述" align="center"></el-table-column>
                        <el-table-column label="操作" align="center">
                            <template slot-scope="scope">
                                <el-button type="primary" size="mini" :disabled="permissionShow.EDIT" @click="handleUpdate(scope.row)">编辑</el-button>
                                <el-button size="mini" type="danger" :disabled="permissionShow.DELETE" @click="handleDelete(scope.row)">删除</el-button>
                            </template>
                        </el-table-column>
                    </el-table>
                    <div class="pagination-container">
                        <el-pagination
                            class="pagiantion"
                            @current-change="handleCurrentChange"
                            :current-page="pagination.currentPage"
                            :page-size="pagination.pageSize"
                            layout="total, prev, pager, next, jumper"
                            :total="pagination.total">
                        </el-pagination>
                    </div>
                    <!-- 新增标签弹层 -->
                    <div class="add-form">
                        <el-dialog title="新增角色" :visible.sync="dialogFormVisible">
                            <template>
                                <el-tabs v-model="activeName" type="card">
                                    <el-tab-pane label="基本信息" name="first">
                                        <el-form ref="dataAddForm" :model="formData" :rules="rules" label-position="right" label-width="100px">
                                            <el-row>
                                                <el-col :span="12">
                                                    <el-form-item label="角色名称" prop="name">
                                                        <el-input v-model="formData.name" />
                                                    </el-form-item>
                                                </el-col>
                                                <el-col :span="12">
                                                    <el-form-item label="关键字" prop="keyword">
                                                        <el-input v-model="formData.keyword" />
                                                    </el-form-item>
                                                </el-col>
                                            </el-row>
                                            <el-row>
                                                <el-col :span="24">
                                                    <el-form-item label="说明">
                                                        <el-input v-model="formData.description" type="textarea"></el-input>
                                                    </el-form-item>
                                                </el-col>
                                            </el-row>
                                        </el-form>
                                    </el-tab-pane>
                                    <el-tab-pane label="权限信息" name="second">
                                        <div class="checkScrol">
                                            <table class="datatable">
                                                <thead>
                                                <tr>
                                                    <th>选择</th>
                                                    <th>权限名称</th>
                                                    <th>关键词</th>
                                                    <th>权限说明</th>
                                                </tr>
                                                </thead>
                                                <tbody>
                                                <tr v-for="c in tableData1">
                                                    <td>
                                                        <input :id="c.id" v-model="permissionIds" type="checkbox" :value="c.id">
                                                    </td>
                                                    <td><label :for="c.id">{{c.name}}</label></td>
                                                    <td><label :for="c.id">{{c.keyword}}</label></td>
                                                    <td><label :for="c.id">{{c.description}}</label></td>
                                                </tr>
                                                </tbody>
                                            </table>
                                        </div>
                                    </el-tab-pane>
                                    <el-tab-pane label="菜单信息" name="third">
                                        <div class="checkScrol">
                                            <table class="datatable">
                                                <thead>
                                                <tr>
                                                    <th>选择</th>
                                                    <th>菜单名称</th>
                                                    <th>优先级</th>
                                                    <th>自关联Id</th>
                                                    <th>描述</th>
                                                </tr>
                                                </thead>
                                                <tbody>
                                                <tr v-for="c in tableData2">
                                                    <td>
                                                        <input :id="c.id" v-model="menuIds" type="checkbox" :value="c.id">
                                                    </td>
                                                    <td><label :for="c.id">{{c.name}}</label></td>
                                                    <td><label :for="c.id">{{c.priority}}</label></td>
                                                    <td><label :for="c.id">{{c.parentMenuId}}</label></td>
                                                    <td><label :for="c.id">{{c.description}}</label></td>
                                                </tr>
                                                </tbody>
                                            </table>
                                        </div>
                                    </el-tab-pane>
                                </el-tabs>
                            </template>
                            <div slot="footer" class="dialog-footer">
                                <el-button @click="dialogFormVisible = false">取消</el-button>
                                <el-button type="primary" @click="handleAdd()">确定</el-button>
                            </div>
                        </el-dialog>
                    </div>

                    <!-- 编辑标签弹层 -->
                    <div class="add-form">
                        <el-dialog title="编辑角色" :visible.sync="dialogFormVisible4Edit">
                            <template>
                                <el-tabs v-model="activeName" type="card">
                                    <el-tab-pane label="基本信息" name="first">
                                        <el-form ref="dataEditForm" :model="formData" :rules="rules" label-position="right" label-width="100px">
                                            <el-row>
                                                <el-col :span="12">
                                                    <el-form-item label="角色名称" prop="name">
                                                        <el-input v-model="formData.name" />
                                                    </el-form-item>
                                                </el-col>
                                                <el-col :span="12">
                                                    <el-form-item label="关键字" prop="keyword">
                                                        <el-input v-model="formData.keyword" />
                                                    </el-form-item>
                                                </el-col>
                                            </el-row>
                                            <el-row>
                                                <el-col :span="24">
                                                    <el-form-item label="说明">
                                                        <el-input v-model="formData.description" type="textarea"></el-input>
                                                    </el-form-item>
                                                </el-col>
                                            </el-row>
                                        </el-form>
                                    </el-tab-pane>
                                    <el-tab-pane label="权限信息" name="second">
                                        <div class="checkScrol">
                                            <table class="datatable">
                                                <thead>
                                                <tr>
                                                    <th>选择</th>
                                                    <th>权限名称</th>
                                                    <th>关键词</th>
                                                    <th>权限说明</th>
                                                </tr>
                                                </thead>
                                                <tbody>
                                                <tr v-for="c in tableData1">
                                                    <td>
                                                        <input :id="c.id" v-model="permissionIds" type="checkbox" :value="c.id">
                                                    </td>
                                                    <td><label :for="c.id">{{c.name}}</label></td>
                                                    <td><label :for="c.id">{{c.keyword}}</label></td>
                                                    <td><label :for="c.id">{{c.description}}</label></td>
                                                </tr>
                                                </tbody>
                                            </table>
                                        </div>
                                    </el-tab-pane>
                                    <el-tab-pane label="菜单信息" name="third">
                                        <div class="checkScrol">
                                            <table class="datatable">
                                                <thead>
                                                <tr>
                                                    <th>选择</th>
                                                    <th>菜单名称</th>
                                                    <th>优先级</th>
                                                    <th>自关联Id</th>
                                                    <th>描述</th>
                                                </tr>
                                                </thead>
                                                <tbody>
                                                <tr v-for="c in tableData2">
                                                    <td>
                                                        <input :id="c.id" v-model="menuIds" type="checkbox" :value="c.id">
                                                    </td>
                                                    <td><label :for="c.id">{{c.name}}</label></td>
                                                    <td><label :for="c.id">{{c.priority}}</label></td>
                                                    <td><label :for="c.id">{{c.parentMenuId}}</label></td>
                                                    <td><label :for="c.id">{{c.description}}</label></td>
                                                </tr>
                                                </tbody>
                                            </table>
                                        </div>
                                    </el-tab-pane>
                                </el-tabs>
                            </template>
                            <div slot="footer" class="dialog-footer">
                                <el-button @click="dialogFormVisible4Edit = false">取消</el-button>
                                <el-button type="primary" @click="handleEdit()">确定</el-button>
                            </div>
                        </el-dialog>
                    </div>
                </div>
            </div>
        </div>
    </body>
    <!-- 引入组件库 -->
    <script src="../js/vue.js"></script>
    <script src="../plugins/elementui/index.js"></script>
    <script type="text/javascript" src="../js/jquery.min.js"></script>
    <script src="../js/axios-0.18.0.js"></script>
    <script>
        var vue = new Vue({
            el: '#app',
            data:{
                pagination: {//分页相关模型数据
					  currentPage: 1,//当前页码
					  pageSize:10,//每页显示的记录数
					  total:0,//总记录数
					  queryString:null//查询条件
				},
				dataList: [],//当前页要展示的分页列表数据
                formData: {},//表单数据
                tableData1:[],//新增和编辑表单中对应的权限列表数据
                tableData2:[],//新增和编辑表单中对应的菜单列表数据
                permissionShow:{},//权限
                permissionIds:[],//权限新增和编辑表单中角色对应的复选框
                menuIds:[],//菜单新增和编辑表单中角色对应的复选框
                dialogFormVisible: false,//增加表单是否可见
                dialogFormVisible4Edit:false,//编辑表单是否可见
                rules: {//校验规则
                    name: [{ required: true, message: '角色名称为必填项', trigger: 'blur' }],
                    keyword: [{ required: true, message: '关键字为必填项', trigger: 'blur' }]
                }
            },
            //钩子函数，VUE对象初始化完成后自动执行
            created() {
                this.findPage();
                this.checkPermission();
            },
            methods: {
                //编辑
                handleEdit() {
                    //表单校验
                    this.$refs["dataEditForm"].validate((validate)=>{
                        //表单校验通过，发送请求
                        if(validate){
                            axios.post("/role/edit.do?permissionIds="+this.permissionIds+"&menuIds="+this.menuIds,this.formData).then((response)=>{
                                //隐藏编辑窗
                                this.dialogFormVisible4Edit=false;
                            //编辑成功弹出成功提示窗口
                            if(response.data.flag){
                                this.$message({
                                    message: response.data.message,
                                    type: 'success'
                                })
                            }else{
                                //编辑失败，弹出错误提示信息
                                this.$message.error(response.data.message);
                            }
                        }).finally(()=>{
                                //重新发送请求查询分页数据
                                this.findPage();
                        })
                        }else{
                            //表单校验失败
                            this.$message.error("表单数据校检失败");
                    return false;
                }
                })
                },
                //添加
                handleAdd () {
                    this.$refs["dataAddForm"].validate((valid) => {
                        if (valid) {
                            axios.post("/role/saveRPM.do?permissionIds="+this.permissionIds+"&menuIds="+this.menuIds,this.formData).then((response)=>{
                                if(response.data.flag){

                                //新增成功后，重新调用分页查询方法
                                this.findPage();
                                //弹出新增成功提示信息
                                this.$message({
                                    showClose: true,
                                    message: response.data.message,
                                    type: 'success'
                                });
                            }else{
                                //新增失败，提示错误信息
                                this.$message({
                                    showClose: true,
                                    message: response.data.message,
                                    type: 'error'
                                });
                            }

                            //关闭新增窗口
                            this.dialogFormVisible=false;
                        }).catch((r)=>{
                                if(r == 'Error: Request failed with status code 403'){
                                    alert("权限不够")
                                }
                            })
                        } else {
                            this.$message({
                            showClose: true,
                            message: "数据校验失败",
                            type: 'error'
                        });
                    return false;
                }
                });
                },
                //查询所有权限
                findAllPermission(){
                    axios.get("/role/findAllPermission.do").then((res)=>{
                        if (res.data.flag){
                            //查询成功
                            //将权限列表数据赋值给模型数据用于将页面展示
                            this.tableData1=res.data.data;
                        } else {
                            this.$message({
                                type:'error',
                                message:res.data.message,
                                center:true
                            });
                        }
                    })
                },
                //查询所有菜单
                findAllMenu(){
                    axios.get("/role/findAllMenu.do").then((res)=>{
                        if (res.data.flag){
                            //查询成功
                            //将菜单列表数据赋值给模型数据用于将页面展示
                            this.tableData2=res.data.data;
                        } else {
                            this.$message({
                                type:'error',
                                message:res.data.message,
                                center:true
                            });
                        }
                    })
                },
                //查询角色与权限的关联关系
                findRoleAndPermission(row){
                    axios.get("/role/findRoleAndPermission.do?id="+row.id).then((res)=>{
                        if (res.data.flag){
                            //查询成功
                            //将权限列表数据赋值给模型数据用于将页面展示
                            this.permissionIds=res.data.data;
                        } else {
                            this.$message({
                                type:'error',
                                message:res.data.message,
                                center:true
                            });
                        }
                    })
                },
                //查询角色与菜单的关联关系
                findRoleAndMenu(row){
                    axios.get("/role/findRoleAndMenu.do?id="+row.id).then((res)=>{
                        if (res.data.flag){
                            //查询成功
                            //将菜单列表数据赋值给模型数据用于将页面展示
                            this.menuIds=res.data.data;
                        } else {
                            this.$message({
                                type:'error',
                                message:res.data.message,
                                center:true
                            });
                        }
                    })
                },
                //分页查询
                findPage() {
                    //构造分页查询条件
                    var params={
                        currentPage:this.pagination.currentPage,
                        pageSize:this.pagination.pageSize,
                        queryString:this.pagination.queryString
                    };

                    //发送ajax请求进行分页查询
                    axios.post("/role/findPage.do",params).then((response)=>{
                        //为模型数据赋值，基于VUE的双向绑定展示到页面
                        this.pagination.total = response.data.total;
                    this.dataList=response.data.rows;
                })
                },
                // 重置表单
                resetForm() {
                    this.formData={};
                    this.permissionIds=[];
                    this.menuIds=[];
                },
                // 弹出添加窗口
                handleCreate() {
                    //发送ajax请求进行权限信息数据回显
                    this.findAllPermission();
                    //发送ajax请求进行菜单信息数据回显
                    this.findAllMenu();
                    this.resetForm();
                    //让第一个选项卡被选中
                    this.activeName='first';
                    this.dialogFormVisible=true;
                    //重置校验条件
                    this.$nextTick(()=>{
                        this.$refs['dataAddForm'].clearValidate(()=>{});
                    });
                },
                // 弹出编辑窗口
                handleUpdate(row) {
                    //发送请求获取用户信息
                    axios.post("/role/findById.do?id="+row.id).then((response)=>{
                        if(response.data.flag){
                        //设置编辑窗口属性，dialogFormVisible4Edit为true表示显示
                        this.dialogFormVisible4Edit=true;
                        //为模型数据设置值，基于VUE双向数据绑定回显到页面
                        this.formData=response.data.data;
                        //发送ajax请求进行权限信息数据回显
                        this.findAllPermission();
                        //发送ajax请求进行菜单信息数据回显
                        this.findAllMenu();
                        //插入角色与权限的关联关系
                        this.findRoleAndPermission(row);
                        //插入角色与权限的关联关系
                        this.findRoleAndMenu(row);
                    }else{
                        this.$message.error("获取数据失败，请刷新当前页面")
                    }
                });
                    //让第一个选项卡被选中
                    this.activeName='first';
                },
                //切换页码
                handleCurrentChange(currentPage) {
                    this.pagination.currentPage=currentPage;
                    this.findPage();
                },
               /* //权限预测
                checkPermission(){
                    //编辑权限测试
                    axios.post("/checkitem/permissionShow.do").then((response)=>{
                        this.permissionShow=response.data.data;
                    })

                },*/
                //权限预测
                checkPermission(){
                    //编辑权限测试
                    axios.post("/user/permissionShow.do?moduleName=ROLE").then((response)=>{
                        this.permissionShow=response.data.data;
                    })
                },
                // 删除
                handleDelete(row) {
                    //提示是否删除
                    this.$confirm("确认删除吗？","提示",{
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                        type: 'warning'}).then(()=>{
                        //点击确定按钮时执行此处代
                        axios.get("/role/delete.do?id="+row.id).then((response)=>{
                        if(response.data.flag){
                        //删除成功
                        this.$message({
                            message:response.data.message,
                            type:'success'
                        })
                        //调用分页，获取最新分页数据
                        this.findPage();

                    }else{
                        //删除失败
                        this.$message.error(response.data.message);
                    }
                })
                })
                }
            }
        });
    </script>
</html>